<template>
	<view>
		<view class="selected" >
			<view class="title">
				已选 : 
				<text style="color: #2979ff;margin-left: 12rpx;">
					{{locationCity==''?'请选择城市':locationCity}}
				</text>
			</view>
		</view>
		<view class="location" >
			<view class="title">当前定位</view>
			<view class="body">
				<view class="left">
					<image src="../../static/navigate.png" mode="widthFix" class="img"></image>
					{{locationCity}}
				</view>
				<!-- <view    class="right">切换城市</view> -->
				<vvSelectArea  class="right" :list="areas" :level="2" v-model="companyArea" placeholder="选择城市"></vvSelectArea>
			</view>
		</view>
		<view class="hot" >
			<view class="title">热门城市</view>
			<view class="body">
				<view class="tag" v-for="(item,index) in hotList" :key="index" @click="clickCity(item)">{{item}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	// 导入库文件

	let QQMapWX = require("../../libs/qqmap-wx-jssdk.min.js");
	// 导入地区数据
	const cityData = require('./area.json')
	// 导入下拉组件
	import vvSelect from '@/components/vv-select/vv-select'
	// 三级联动的组件
	import vvSelectArea from '@/components/vv-select-area/vv-select-area'
	export default {
		components:{
			vvSelect,
			vvSelectArea
		},
		onLoad() {
			// 获取当前的地址信息
			this.findLocation()
		},
		data() {
			return {
				companyArea: '',
				areas: cityData, // 类别list
				locationCity: '',
				hotList: ['杭州市', '郑州市', '北京市', '上海市', '广州市', '深圳市']
			};
		},
		watch:{
			// 值,就会触发此方法,
			companyArea:{
				handler: function (arr){
					const sheng = cityData.find((item) => {
						return item.id == arr[0]
					})
					const city = sheng.childs.find((item) => {
						return item.id == arr[1]
					})
					this.locationCity = city.name;
					uni.setStorageSync("locationCity", this.locationCity)
				},
				// immediate:true,
				deep:true
			}
		},
		methods: {
			clickCity(item){
				// 将城市保存到本地
				this.locationCity =item
				uni.setStorageSync("locationCity", item)
			},
			findLocation() {
				// console.log(cityData)
				// 创建sdk实例
				const qqmapsdk = new QQMapWX({
					key: 'UVMBZ-YPSR4-ZZ5UH-FA77V-I2MN6-Y7FEH' //申请的key
				});
				// 执行sdk 获取当前所在位置的城市
				qqmapsdk.reverseGeocoder({
					location,
					success: (response) => {
						let info = response.result;
						console.log(info)
						this.locationCity = info.address_component.city;
						uni.setStorage({
							key:"add_info",
							data:JSON.stringify(info.ad_info),
							fail: () => {
								uni.showToast({
									icon:"error",
									title:"获取数据失败"
								})
							}
						})
						// 将城市保存到本地
						uni.setStorageSync("locationCity", info.address_component.city)
					},
				});
			}
		},
	}
</script>

<style lang="scss" scoped>
	.selected{
		.title {
		    font-size: 30rpx;
		    color: $u-main-color;
			margin: 30rpx 20rpx;
			font-weight: 600;
			margin-left: 30rpx;
		}
	}
	.location{
		background: #FFFFFF;
		border-radius: 18rpx;
		padding: 3rpx 0;
		margin: 20rpx;
		.title {
		    font-size: 22rpx;
		    color: $u-tips-color;
			margin: 30rpx 20rpx;
		}
		
		.body{
			margin: 30rpx 20rpx;
			display: flex;
			justify-content: space-between;
			.left{
				font-size: 32rpx;
				font-weight: 800;
				display: flex;
				justify-content: center;
				align-items: center;
				.img{
					width: 35rpx;
					margin-right: 12rpx;
				}
			}
			.right{
				color: #2979ff;
				font-weight: 600;
				margin-right: 10rpx;
			};
		}
	}
	.hot{
		.title {
		    font-size: 30rpx;
		    color: $u-main-color;
			margin: 30rpx 20rpx;
			font-weight: 600;
			margin-left: 30rpx;
		}
		
		.body{
			background: #FFFFFF;
			padding-bottom: 15rpx;
			border-radius: 18rpx;
			margin: 20rpx;
			.tag {
				display: inline-block;
				width: 137rpx;
				height: 75rpx;
				line-height: 75rpx;
			    font-size: 26rpx;
			    color: $u-content-color;
			    margin: 20rpx 20rpx 5rpx 20rpx;
				padding: 5rpx 10rpx;
				text-align: center;
				background-color: $u-bg-color;
				border-radius: 12rpx;
			}
		}
	}
</style>
